var t = getApp(), e = t.requirejs("core"), foxui = t.requirejs("foxui");
Page({
    data: {
        list: {},
        realprice: "",
        zheprice: "",
        data: {
            dispatchtype: 0,
            remark: ""
        },
        areaDetail: {
            detail: {
                realname: "",
                mobile: "",
                areas: "",
                street: "",
                address: ""
            }
        },
        merchid: 0,
        remark: "",
        onFocus: !1,
        isShowText: !0,
        cover: false,
        coupon: false,
        count: 0,
        couponName: [],
        couponList: [],
        delivery_fee: 0,
    },
    onLoad: function(e) {
        var that = this;
        that.setData({
            options: e,
        })
        if(e.type==1) that.setData({ remark: "门店自配送请备注详细收货地址", });
        else that.setData({ remark: "请输入备注信息", });
    },
    onShow: function() {
        var i = this;
        t.getCache("isIpx") ? i.setData({
            isIpx: !0,
            iphonexnavbar: "fui-iphonex-navbar",
            paddingb: "padding-b"
        }) : i.setData({
            isIpx: !1,
            iphonexnavbar: "",
            paddingb: ""
        }),
        i.setData({
            imgUrl: t.globalData.approot
        });
        wx.getLocation({
            type: "wgs84",
            success: function(res) {
                i.setData({
                    "options.lat": res.latitude,
                    "options.lng": res.longitude,
                }), 
                i.getInfo();
            },
            fail: function(t) {
                t.alert("取消位置信息将无法定位商家距离！"), 
                i.setData({
                    "options.lat": res.latitude,
                    "options.lng": res.longitude,
                }), 
                i.getInfo();
            }
        })
    },
    getInfo: function() {
        var th = this;
        e.get("merch/create", th.data.options, function(res) {
            
            th.setData({
                list: res,
                submit: 1,
                realprice: res.realprice,
                zheprice: res.realprice,
                show: !0,
            }), th.getCoupon(), th.checkwmpreorder();
            // if(res.error==0){
            // }else{
            //     e.alert(res.error_message);
            //     th.setData({ submit: 0 })
            // }
        });
    },
    checkwmpreorder: function(){
        var th = this, r = t.getCache("orderAddress");
        r && (th.setData({
            "list.address": r
        }));
        var data = {
            o_type:th.data.list.o_type,
            addressid:th.data.list.address ? th.data.list.address.id : 0,
            shanghuid:th.data.list.merchid,
            realprice:th.data.list.realprice,
            weight:th.data.list.weight,
            allgoods:th.data.list.goods,
        };
        // console.log(data);
        e.get('merch/create/checkwmpreorder',data,function(res){
            if(res.error==0){
                th.setData({
                    delivery_fee:res.delivery_fee
                })
            }else{
                e.alert(res.error_message);
                th.setData({ submit: 0 })
            }
        })
    },
    getCoupon: function() {
        var t = this, shid = t.data.options.merchid, price = t.data.realprice;
        e.get("merch/get_coupon_use", {shanghuid: shid, money: price}, function(res) {
            var list = res.list;
            for (var i = 0; i < list.length; i++) {
                list[i].select = false;
            }
            res.list = list;
            t.setData({
                count: res.count,
                couponList: res.list,
            })
        });
    },
    choice: function() {
        this.setData({
            cover: true,
            coupon: true,
        })
    },
    receive: function(p) {
        var _t = this, couponid = p.currentTarget.dataset.couponid,
            backtype = p.currentTarget.dataset.type,
            index = p.currentTarget.dataset.index,
            couponList = _t.data.couponList,
            zheprice = _t.data.zheprice,
            zhe = "", couponName = "";
        for(var i = 0; i < this.data.couponList.length; i++){
            if(index == i){
                couponList[i].select = true;
            } else {
                couponList[i].select = false
            }
        }
        if(backtype==0){
            zhe = couponList[index].deduct;
            couponName = "减"+couponList[index].deduct;
            zheprice = parseFloat(zheprice) - parseFloat(zhe);
        }else{
            zhe = couponList[index].discount;
            couponName = couponList[index].couponname;
            zheprice = parseFloat(zheprice) * parseFloat(zhe/10);
        }
        zheprice = zheprice.toFixed(2);
        _t.setData({
            couponid: couponid,
            realprice: zheprice,
            couponName: couponName,
            cover: false,
            coupon: false,
        }),
        _t.setData(this.data);
    },
    cancel: function() {
        this.setData({
            cover: false,
            coupon: false,
        })
    },
    confirm: function() {
        var _t = this, list = _t.data.couponList,
            type = _t.data.couponType, index = _t.data.couponIndex;
        _t.setData({
            cover: false,
            coupon: false,
        })
    },
    url: function(t) {
        var a = e.pdata(t).url;
        wx.navigateTo({
            url: a
        });
    },
    call: function(p) {
        let phone = p.currentTarget.dataset.tel;
        wx.makePhoneCall({
            phoneNumber: phone,
            success:function() { console.log("成功！"); },
            fail:function() { console.log("失败！"); }
        })
    },
    onShowTextarea: function() {
        var that = this,
        remarks = that.data.remark;
        if(remarks=="门店自配送请备注详细收货地址" || remarks=="请输入备注信息"){
            that.setData({
                remark: ""
            })
        }
        that.setData({
            isShowText: !1,
            onFacus: !0
        });
    },
    onShowText: function(p) {
        var e = p.detail.value;
        if(this.data.options.type==1){
            "" == e && (e = "门店自配送请备注详细收货地址"), this.setData({
                isShowText: !0,
                onFacus: !1,
                remark: e
            });
        }else{
            "" == e && (e = "请输入备注信息"), this.setData({
                isShowText: !0,
                onFacus: !1,
                remark: e
            });
        }
    },
    dataChange: function(p) {
        var e = p.detail.value;
        this.setData({
            remark: e
        });
    },
    submit: function() {
        var  that = this, t = that.data;
        if(t.remark=="门店自配送请备注详细收货地址" || t.remark=="请输入备注信息")
        t.remark = "";
        var c = {
            id: t.options.id ? t.options.id : 0,
            goods: t.list.goods,
            gdid: t.options.gdid,
            dispatchtype: t.data.dispatchtype,
            fromcart: t.list.fromcart,
            carrierid: 1 == t.data.dispatchtype && t.list.carrierInfo ? t.list.carrierInfo.id : 0,
            addressid: t.list.address ? t.list.address.id : 0,
            carriers: 1 == t.data.dispatchtype || t.list.isvirtual || t.list.isverify ? {
                carrier_realname: t.list.member.realname,
                carrier_mobile: t.list.member.mobile,
                realname: t.list.carrierInfo.realname,
                mobile: t.list.carrierInfo.mobile,
                storename: t.list.carrierInfo.storename,
                address: t.list.carrierInfo.address
            } : "",
            remark: t.remark,
            cardid: t.cardid,
            invoicename: t.list.invoicename,
            submit: !0,
            packageid: t.list.packageid,
            receipttime: t.receipttime,
            fromquick: t.fromquick,
            merchid: t.options.merchid,
            o_type: t.options.type,
            couponid: t.couponid,
            cart_type: t.options.cart_type,
            delivery_fee: t.delivery_fee ? t.delivery_fee : 0,
        };
        if(t.options.type==2){
            if(!c.addressid) {
                e.alert("请选择地址");
                return false;
            }
        }
        if(!c.remark && t.options.type==1) {
            e.alert("请备注详细收货地址");
            return false;
        }
        that.setData({
            submit: !0
        });
        e.post("merch/create/submit", c, function(t) {
            that.setData({
                submit: !1
            })
            0 == t.error ? wx.redirectTo({
                url: "/pages/order/pay/index?id="+t.orderid+"&cart_type="+that.data.options.cart_type
            }) : e.alert(t.message);
        }, !0);
    },
});